#include<vector>
#include <stack>

class Solution {
public:
    int maxWidthRamp(std::vector<int>& A) {
        std::stack<int> stack_;
        for (int i = 0; i < A.size(); i++) {
            if (stack_.empty() || A[stack_.top()] > A[i])
                stack_.push(i);
        }
        int res = 0;
        for (int i = A.size() - 1; i >= res; i--) {
            if (!stack_.empty() && stack_.top() == i)
                stack_.pop();

            while (!stack_.empty() && A[stack_.top()] <= A[i]) {
                int tmp = stack_.top();
                stack_.pop();
                if (res < i - tmp)
                    res = i - tmp;
            }
        }
        return res;
    }
};